package awesome.Manager;

import java.sql.ResultSet;
import java.sql.SQLException;

import awesome.Threading.BidThreadManager;

public class BidManager extends Manager {

	private static BidThreadManager bidThread;

	public BidManager() {
		super("bid");
	}

	public double getCurrentBid(int iid) {
		String query = "SELECT amount FROM bid WHERE iid = " + iid;
		ResultSet rs = db.getResultSet(query);
		float currentAmount = 0.0f;
		float amount;
		if (rs != null) {
			try {
				while (rs.next()) {
					amount = rs.getFloat("amount");
					if (amount > currentAmount) {
						currentAmount = amount;
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return currentAmount;
	}

	public void submitBid(int uid, int iid, float amount) {

		double currentBid = getCurrentBid(iid);

		if (currentBid < amount) {
			String query = "INSERT INTO bid " + "VALUES ('" + uid + "','" + iid
					+ "','" + amount + "','Null')";

			db.execute(query);

			if (bidThread == null) {
				bidThread = new BidThreadManager();
				bidThread.start();
			}

			bidThread.addBid(uid, iid, amount);
		}
	}
}
